<template>
  <div class="cover" @click="close" v-if="show_login === 1">
    <div class="login-content" @click.stop>
      <div class="login-close" @click="close">
        <i class="fa fa-close"></i>
      </div>
      <div class="login-body">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="登录" name="first">
            <!-- <form action="/user/login" method="POST"> -->
              <div class="good-group">
                <el-input class="good-control" name="username"  placeholder="请输入登录用户名/邮箱" v-model="username">
                  <template slot="prepend">
                    <i class="fa fa-user"></i>
                  </template>
                </el-input>
              </div>
              <div class="good-group">
                <el-input class="good-control" name="password" type="password" placeholder="请输入登录密码" v-model="password">
                  <template slot="prepend">
                    <i class="fa fa-lock"></i>
                  </template>
                </el-input>
              </div>
              <div class="good-group remember-or-reset clearfix">
                <label class="remember-txt fl"><input name="remember" checked type="checkbox">下次自动登录</label>
                <a class="fr" href="">忘记密码？</a>
              </div>
              <el-button @click="login" class="login-btn" type="primary">登录</el-button>
            <!-- </form> -->
          </el-tab-pane>
          <el-tab-pane label="注册" name="second">
            <form method="POST" v-show="!reg_success">
              <div class="good-group">
                <el-input class="good-control" name="username" placeholder="请输入注册邮箱" @blur="checkUser" v-model="regUsername">
                  <template slot="prepend">
                    <i class="fa fa-user"></i>
                  </template>
                </el-input>
              </div>
              <div class="good-group">
                <el-input class="good-control" name="password" type="password" placeholder="请输入密码" v-model="regPassword">
                  <template slot="prepend">
                    <i class="fa fa-lock"></i>
                  </template>
                </el-input>
              </div>
              <el-button @click="registered" class="register-btn" type="primary">注册</el-button>
            </form>
            <div class="reg-success" v-show="reg_success">
              <div class="user-header">
                <i class="fa fa-user"></i>
              </div>
              <p class="reg-tips">恭喜您注册成功！</p>
              <el-button @click="regLogin" type="primary">立即登录</el-button>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="tips" v-if="tips">
        <i class="el-icon-info"></i>
        <span class="tips-txt">{{ tips }}</span>
      </div>
    </div>
  </div>
</template>
<script>
  // import axios from 'axios'
  import { mapGetters, mapMutations, mapActions } from 'vuex'
  export default {
    data() {
      return {
        activeName: 'first',
        username: '',
        password: '',
        regUsername: '',
        regPassword: '',
        tips: '',
        reg_success: false
      };
    },
    computed: {
      ...mapGetters([
          'show_login',
        ])
    },
    watch: {
      username: function () {
        this.tips = ''
      },
      password: function () {
        this.tips = ''
      },
      regUsername: function () {
        this.tips = ''
      },
      regPassword: function () {
        this.tips = ''
      }
    },
    methods: {
      handleClick(tab, event) {
        this.tips = ''
      },
      close() {
        this.showLogin(0)  
      },
      ...mapActions({
        userLogin: 'userLogin',
        userRegister: 'userRegister',
      }),
      ...mapMutations({
        showLogin: 'SHOW_LOGIN',
      }),
      registered(){

        if (!this.regUsername) {
          this.tips = '请输入注册邮箱'
          return
        }

        if (!this.regPassword) {
          this.tips = '请输入密码'
          return
        }

        if (!this.regUsername.includes('@')) {
          this.tips = '您输入的邮箱不合法'
          return
        }

        let self = this
        this.userRegister({
          email: self.regUsername,
          password: self.regPassword
        }).then((res) => {
          let { code, data = {} } = res
          if (code == 0) {
            localStorage.setItem('user_info', JSON.stringify(data))
            this.reg_success = true
          }
          else if (code == 1) {
            self.tips = res.msg
          }
        })
      },
      login() {

        if (!this.username) {
          this.tips = '请输入登录用户名或邮箱'
          return
        }

        if (!this.password) {
          this.tips = '请输入登录密码'
          return
        }

        let self = this
        self.userLogin({
          email: self.username,
          password: self.password
        }).then((res) => {
          console.log(res)
          let { code, data = {} } = res
          if (code == 0) {
            localStorage.setItem('user_info', JSON.stringify(data))
            window.location.reload()
          } else if (code == 1) {
            self.tips = res.msg
          }
        })
      },
      checkUser() {
        if (this.regUsername && !this.regUsername.includes('@')) {
          this.tips = '您输入的邮箱不合法'
        } else {
          this.tips = ''
        }
      },
      regLogin() {
        window.location.reload()
      }
    }
  };
</script>
<style @scoped lang="less">
  .login-content {
    position: relative;
    z-index: 100;
    background-color: #fff;
    padding: 30px 25px 40px;
    box-sizing: border-box;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
    border-radius: 5px;
    margin: 100px auto;

    .login-close {
      position: absolute;
      right: 8px;
      top: 8px;
      border-radius: 5px;
      width: 20px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      border-radius: 10px;
      background-color: #fff;
      transition: 0.2s;
      cursor: pointer;

      i {
        font-size: 16px;
      }

      &:hover {
        transform: rotateZ(180deg);
        background-color: #409EFF;
      }

      &:hover i {
        color: #fff;
      }
    }
    
    .login-body {
      width: 100%;
      height: 100%;

      .el-tabs__nav {
        width: 100%;
      }
      .el-tabs__item {
        width: 50%;
      }
      .el-tabs__active-bar {
        width: 50%;
      }
      .good-group {
        margin-bottom: 15px;
      }
      form {
        margin-top: 10px;
      }
      .login-btn,
      .register-btn {
        width: 100%;
      }

      .remember-or-reset {
        font-size: 12px;
      }
      

      .good-control input {
        height: 42px;
        padding: 7px 12px;
        font-size: 16px;
        line-height: 1.5;
      }

      .reg-success {
        .user-header {
          margin-top: 30px;
          font-size: 50px;
        }

        .reg-tips {
          margin: 10px auto;
          font-size: 18px;
          line-height: 1.5;
          color: #13181E
        }
      }

    }
    
    .tips {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      line-height: 40px;
      height: 40px;
      color: #f66495;
      text-align: center;
    }
    
  }
  

  @media (min-width: 768px) {
    .login-content {
      width: 350px;
    }
  }
  @media (max-width: 768px) {
    .login-content {
      width: 90%;
      margin: 30px auto;
    }
  }
</style>







